/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */

package com.yunbiji.controller;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yunbiji.interceptor.MyCaptchaRenderInterceptor;
import com.yunbiji.interceptor.RegisterInterceptor;
import com.yunbiji.model.Users;
import com.yunbiji.validate.RegisterValidate;
/**
 * 注册操作类
 * @author 聂道鑫
 *
 */
public class RegisterController extends Controller
{
	private static final Logger logger = Logger.getLogger(RegisterController.class);
	
	/**
	 * 返回到注册界面
	 */
	public void index()
	{

		render("register.html");
	}
	/**
	 * 返回登陆界面
	 */
	public void login()
	{
		render("login.html");
	}
  @Before({RegisterValidate.class,RegisterInterceptor.class,MyCaptchaRenderInterceptor.class})
  /**
   * 获取注册信息并写入数据库
   */
	public void reg()
	{
		String name= getPara("name");
		String email= getPara("email");
		String qq= getPara("qq");
		String tel= getPara("tel");
		String passwd =getPara("passwd");
		String repasswd= getPara("repasswd");
	try
	{
		if(!passwd.equals(repasswd))
		{
			keepPara("name","email","qq","tel");
			setAttr("errorReg", "两次密码不相同请重新输入!");
			render("register.html");
		}
		else
		{
			if(Users.dao.reg(name, email, qq, tel, passwd))
			{
			setAttr("alertInfo", "注册成功!         3秒后返回登录界面!");
			render("errors/alert.html");
			getResponse().setHeader("Refresh", "3;URL=login");
			}
			
		}
	}
	catch(Exception e)
	{
		logger.error("注册功能出现错误");
		setAttr("alertInfo", "注册出错！请重新注册.2秒后自动返回注册界面！");
		render("errors/alert.html");
		getResponse().setHeader("Refresh", "2;URL=index");
	}


	}

}
